package com.itheima.controller;

import com.itheima.domain.UmsMenu;
import com.itheima.domain.UmsRole;
import com.itheima.manager.UmsMenuManager;
import com.itheima.manager.UmsResoureManager;
import com.itheima.manager.UmsRoleManager;
import com.itheima.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UmsRoleController
 * @date 2022/11/12 14:41
 */
@RestController
public class UmsRoleController {

    @Autowired
    UmsRoleManager umsRoleManager;

    @Autowired
    UmsMenuManager umsMenuManager;

    @Autowired
    UmsResoureManager umsResoureManager;

    // 获取所有角色
    @RequestMapping(value = "/role/listAll",method = RequestMethod.GET)
    public R listAll(){
        return umsRoleManager.listAll();
    }

    // 获取指定用户的角色
    @RequestMapping(value = "/admin/role/{adminId}",method = RequestMethod.GET)
    public R findByRoleList(@PathVariable Long adminId){
        return umsRoleManager.findByRoleList(adminId);
    }

    // 给用户分配角色
    @RequestMapping(value = "/admin/role/update",method = RequestMethod.POST)
    public R allocationRole(Long adminId, @RequestParam(value = "roleIds") List<Long> roleIds){
        return umsRoleManager.allocationRole(adminId,roleIds);
    }

    // 根据角色名称分页获取角色列表
    @RequestMapping(value = "/role/list",method = RequestMethod.GET)
    public R getList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword",required = false) String keyword
    ){
        return umsRoleManager.getList(pageNum,pageSize,keyword);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/role/listMenu/{roleId}",method = RequestMethod.GET)
    public R<List<UmsMenu>> getRoleListMenu(@PathVariable Long roleId){

        List<UmsMenu> menu = umsRoleManager.getRoleListMenu(roleId);
        return R.success(menu);
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @RequestMapping(value = "/role/allocMenu",method = RequestMethod.POST)
    public R saveAllocMenu(Long roleId,@RequestParam(value = "menuIds") List<Long> menuIds){
        return umsRoleManager.saveAllocMenu(roleId,menuIds);
    }

    /**
     * 给角色分配资源
     * @param resourceIds
     * @param roleId
     * @return
     */
    @PostMapping("/role/allocResource")
    public R allocResource(@RequestParam(value = "resourceIds") List<Integer> resourceIds,Long roleId){
        return umsRoleManager.allocResource(resourceIds,roleId);
    }
    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/role/listResource/{roleId}",method = RequestMethod.GET)
    public R listResource(@PathVariable Long roleId){
        return umsResoureManager.listResource(roleId);
    }

    /**
     * 修改角色
     * @param id
     * @param umsRole
     * @return
     */
    @RequestMapping(value = "/role/update/{id}",method = RequestMethod.POST)
    public R roleUpdate(@PathVariable Long id, @RequestBody UmsRole umsRole){
        return umsRoleManager.roleUpdate(id,umsRole);
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @RequestMapping(value = "/role/delete",method = RequestMethod.POST)
    public R delete(@RequestParam("ids") List<Long> ids){
        return umsRoleManager.delete(ids);
    }

    /**
     * 添加角色
     * @param umsRole
     * @return
     */
    @PostMapping("/role/create")
    public R create(@RequestBody UmsRole umsRole){
        return umsRoleManager.create(umsRole);
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/role/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id,Integer status){
        return umsRoleManager.updateStatus(id,status);
    }
}
